<?php

$IS_HEZUO = false;
if ($argv[1] == 'hezuo') {
    $IS_HEZUO = true;
}
$IS_TEST = false;
if ($argv[1] == 'test') {
    $IS_TEST = true;
}

require_once dirname(__FILE__) . '/init.php';
$arrUnSetDomains = getArrayFromFile(dirname(__FILE__) . '/unsetdomain.txt');

$arr_installed_subdomains = get_all_installed_site_subdomains();
$arr_installed_maindomains = get_main_domains_by_subdomains($arr_installed_subdomains);
$arr_installed_maindomains = array_unique(array_filter($arr_installed_maindomains));
$arrVpsData = require_once dirname(__FILE__) . '/input/vpslist.config.php';
$arrVpsShouRuData = array();

$arrExtShouRuData = array();

$tableHead = '<hr />所有域名过期时间统计 Stat By [' . getLocalIp() . ']<hr />
<table border="1" width="1300" bordercolorlight="#C0C0C0" bordercolordark="#C0C0C0" style="border-collapse: collapse;font-size: 12px;">
	<tr>
		<td width="150"><b><font size="2">域名</font></b></td>
		<td width="70"><b><font size="2">7天1</font></b></td>
		<td width="70"><b><font size="2">30天1</font></b></td>
		<td width="70"><b><font size="2">7天2</font></b></td>
		<td width="70"><b><font size="2">30天2</font></b></td>
		<td width="70"><b><font size="2">7天3</font></b></td>
		<td width="70"><b><font size="2">30天3</font></b></td>
		<td width="100"><b><font size="2">过期时间</font></b></td>
		<td width="60"><b><font size="2">域名剩余</font></b></td>
		<td><b><font size="2">IP地址</font></b></td>
		<td><b><font size="2">DNS</font></b></td>
		<td width="60"><b><font size="2">机器剩余</font></b></td>
		<td width="50"><b><font size="2">IDC</font></b></td>
	</tr>
';

$arrTimeCompTR = array();

if ($IS_HEZUO) {
    $arr_installed_maindomains = getArrayFromFile(dirname(__FILE__) . '/hezuodomain.txt');
}

foreach ($arr_installed_maindomains as $maindomain) {
    if (in_array($maindomain, $arrUnSetDomains)) {
        continue;
    }
    if (in_array(str_replace('www.', '', $maindomain), $arrUnSetDomains)) {
        continue;
    }
    $domain = $maindomain;
    $arrRegisterInfo = php_sinaapp_whois($maindomain);
    #过期时间
    $exp_int_time = strtotime($arrRegisterInfo['cxpiration_date']);
    $exp_date = date('Y-m-d', $exp_int_time);
    #剩余天数
    $time_domain = $exp_int_time - time();
    $days_domain = floor($time_domain / 86400);
    #机器IP

    //$ip = $hostip = get_hostip_by_name($maindomain);
	$ip = $hostip = $arrRegisterInfo['_ipaddress'];

    #收入
    $day7_1 = get_domain_usd($maindomain, '7day_1');
    $day7_2 = get_domain_usd($maindomain, '7day_2');
    $day7_3 = get_domain_usd($maindomain, '7day_3');
    $day30_1 = get_domain_usd($maindomain, '30day_1');
    $day30_2 = get_domain_usd($maindomain, '30day_2');
    $day30_3 = get_domain_usd($maindomain, '30day_3');
    #dns_server
    $dns_server = $arrRegisterInfo['name_servers'][0];

    $arrVpsShouRuData[$hostip]['day7_1']+=$day7_1;
    $arrVpsShouRuData[$hostip]['day7_2']+=$day7_2;
    $arrVpsShouRuData[$hostip]['day7_3']+=$day7_3;
    $arrVpsShouRuData[$hostip]['day30_1']+=$day30_1;
    $arrVpsShouRuData[$hostip]['day30_2']+=$day30_2;
    $arrVpsShouRuData[$hostip]['day30_3']+=$day30_3;
    $arrVpsShouRuData[$hostip]['usd'] = $arrVpsShouRuData[$hostip]['day7_1'];
    $arrVpsShouRuData[$hostip]['ip'] = $hostip;

    $arrMainDomainInfo1 = explode('.', $maindomain);
    $arrExtShouRuData[$arrMainDomainInfo1[1]]['day7_1']+=$day7_1;
    $arrExtShouRuData[$arrMainDomainInfo1[1]]['day7_2']+=$day7_2;
    $arrExtShouRuData[$arrMainDomainInfo1[1]]['day7_3']+=$day7_3;
    $arrExtShouRuData[$arrMainDomainInfo1[1]]['day30_1']+=$day30_1;
    $arrExtShouRuData[$arrMainDomainInfo1[1]]['day30_2']+=$day30_2;
    $arrExtShouRuData[$arrMainDomainInfo1[1]]['day30_3']+=$day30_3;
    $arrExtShouRuData[$arrMainDomainInfo1[1]]['total_count'] ++;

    $BgWarning = false;
    if ($days_domain < 30) {
        $days_domain = '<font color="red"><b>' . $days_domain . '</b></font>';
        $BgWarning = true;
    }

    $bg = '';
    if ($BgWarning) {
        $bg = 'bgcolor="#CFCCFC"';
    }

    if ($day7_1 > $day7_2) {
        $day71flag = '<font color="red">↑</font>';
    }
    if ($day7_1 < $day7_2) {
        $day71flag = '<font color="green">↓</font>';
    }
    if ($day7_1 == $day7_2) {
        $day71flag = '<font color="blue">-</font>';
    }

    if ($day7_2 > $day7_3) {
        $day72flag = '<font color="red">↑</font>';
    }
    if ($day7_2 < $day7_3) {
        $day72flag = '<font color="green">↓</font>';
    }
    if ($day7_2 == $day7_3) {
        $day72flag = '<font color="blue">-</font>';
    }

    if ($day30_1 > $day30_2) {
        $day301flag = '<font color="red">↑</font>';
    }
    if ($day30_1 < $day30_2) {
        $day301flag = '<font color="green">↓</font>';
    }
    if ($day30_1 == $day30_2) {
        $day301flag = '<font color="blue">-</font>';
    }

    if ($day30_2 > $day30_3) {
        $day302flag = '<font color="red">↑</font>';
    }
    if ($day30_2 < $day30_3) {
        $day302flag = '<font color="green">↓</font>';
    }
    if ($day30_2 == $day30_3) {
        $day302flag = '<font color="blue">-</font>';
    }

    $TR = '
		<tr ' . $bg . '>
		<td width="175">' . $domain . '</td>
		<td width="50">' . $day7_1 . '[' . $day71flag . ']</td>
		<td width="50">' . $day30_1 . '[' . $day301flag . ']</td>
		<td width="50">' . $day7_2 . '[' . $day72flag . ']</td>
		<td width="50">' . $day30_2 . '[' . $day302flag . ']</td>
		<td width="50">' . $day7_3 . '</td>
		<td width="50">' . $day30_3 . '</td>
		<td width="75">' . $exp_date . '</td>
		<td width="30">' . $days_domain . '</td>
		<td>' . $ip . '</td>
		<td>' . $dns_server . '</td>
		<td width="30">' . $days_vps . '</td>
		<td width="30">' . $arrVpsData[$ip]['idc'] . '</td>
	</tr>	
	';

    $arrTableTR[$domain]['tr'] = $TR;
    $arrTableTR[$domain]['usd'] = get_domain_usd($domain, '7day_1');

    $arrTimeCompTR[$domain]['tr'] = $TR;
    $arrTimeCompTR[$domain]['days'] = $days_domain;


    if ($BgWarning) {
        $arrTableRr_Warning[$domain]['tr'] = $TR;
        $arrTableRr_Warning[$domain]['usd'] = get_domain_usd($domain, '7day_1');
    }
}

usort($arrTableTR, 'arrCmpUsdCount');
foreach ($arrTableTR as $TR) {
    $tableBody.=$TR['tr'];
}

$tableFoot = '</table>';
$html = $tableHead . $tableBody . $tableFoot;
$fileReport = dirname(__FILE__) . '/data/report/' . date('Y-m-d') . '/Maindomains-Money-Report.html';
FCheckDir($fileReport);
file_put_contents($fileReport, $html);

if ($IS_HEZUO) {
    post_email_by_sae('合作建站-域名收入排行统计[' . date('Y-m-d H:i:s') . ']', $html, $arrHezuoUsers);
    post_email_by_smtp('合作建站-域名收入排行统计[' . date('Y-m-d H:i:s') . ']', $html, $arrHezuoUsers);
} else {

    post_email_by_sae('域名收入排行统计[' . date('Y-m-d H:i:s') . ']', $html, $arrUsers);
    post_email_by_smtp('域名收入排行统计[' . date('Y-m-d H:i:s') . ']', $html, $arrUsers);
}

#统计出现警告要过期的域名
$tableBody = '';
usort($arrTableRr_Warning, 'arrCmpUsdCount');
foreach ($arrTableRr_Warning as $TR) {
    $tableBody.=$TR['tr'];
}

$html = $tableHead . $tableBody . $tableFoot;
$fileReport = dirname(__FILE__) . '/data/report/' . date('Y-m-d') . '/Maindomains-Warning-Time-Report.html';
FCheckDir($fileReport);
file_put_contents($fileReport, $html);

if ($IS_HEZUO) {
    post_email_by_sae('合作建站-域名过期时间统计警告[' . date('Y-m-d H:i:s') . ']', $html, $arrHezuoUsers);
    post_email_by_smtp('合作建站-域名过期时间统计警告[' . date('Y-m-d H:i:s') . ']', $html, $arrHezuoUsers);
} else {
    post_email_by_sae('域名过期时间统计警告[' . date('Y-m-d H:i:s') . ']', $html, $arrUsers);
    post_email_by_smtp('域名过期时间统计警告[' . date('Y-m-d H:i:s') . ']', $html, $arrUsers);
}


#统计所有的域名
$tableBody = '';
usort($arrTimeCompTR, 'arrCmpTimeRemain');
foreach ($arrTimeCompTR as $TR) {
    $tableBody.=$TR['tr'];
}

$html = $tableHead . $tableBody . $tableFoot;
$fileReport = dirname(__FILE__) . '/data/report/' . date('Y-m-d') . '/Maindomains-Global-Time-Report.html';
FCheckDir($fileReport);
file_put_contents($fileReport, $html);

if ($IS_HEZUO) {
    post_email_by_sae('合作建站-所有域名过期时间统计[' . date('Y-m-d H:i:s') . ']', $html, $arrHezuoUsers);
    post_email_by_smtp('合作建站-所有域名过期时间统计[' . date('Y-m-d H:i:s') . ']', $html, $arrHezuoUsers);
} else {

    post_email_by_sae('所有域名过期时间统计[' . date('Y-m-d H:i:s') . ']', $html, $arrUsers);
    post_email_by_smtp('所有域名过期时间统计[' . date('Y-m-d H:i:s') . ']', $html, $arrUsers);
}


$tableHead = '<hr />VPS-IP收入统计 Stat By [' . getLocalIp() . ']<hr />
<table border="1" width="1300" bordercolorlight="#C0C0C0" bordercolordark="#C0C0C0" style="border-collapse: collapse;font-size: 12px;">
	<tr>
		<td width="150"><b><font size="2">机器IP</font></b></td>
		<td width="70"><b><font size="2">7天1</font></b></td>
		<td width="70"><b><font size="2">30天1</font></b></td>
		<td width="70"><b><font size="2">7天2</font></b></td>
		<td width="70"><b><font size="2">30天2</font></b></td>
		<td width="70"><b><font size="2">7天3</font></b></td>
		<td width="70"><b><font size="2">30天3</font></b></td>
		<td width="60"><b><font size="2">机器过期</font></b></td>
		<td width="50"><b><font size="2">IDC</font></b></td>
	</tr>
';

usort($arrVpsShouRuData, 'arrCmpUsdCount');

foreach ($arrVpsShouRuData as $ip => $item) {
    extract($item);

    if ($day7_1 > $day7_2) {
        $day71flag = '<font color="red">↑</font>';
    }
    if ($day7_1 < $day7_2) {
        $day71flag = '<font color="green">↓</font>';
    }
    if ($day7_1 == $day7_2) {
        $day71flag = '<font color="blue">-</font>';
    }

    if ($day7_2 > $day7_3) {
        $day72flag = '<font color="red">↑</font>';
    }
    if ($day7_2 < $day7_3) {
        $day72flag = '<font color="green">↓</font>';
    }
    if ($day7_2 == $day7_3) {
        $day72flag = '<font color="blue">-</font>';
    }

    if ($day30_1 > $day30_2) {
        $day301flag = '<font color="red">↑</font>';
    }
    if ($day30_1 < $day30_2) {
        $day301flag = '<font color="green">↓</font>';
    }
    if ($day30_1 == $day30_2) {
        $day301flag = '<font color="blue">-</font>';
    }

    if ($day30_2 > $day30_3) {
        $day302flag = '<font color="red">↑</font>';
    }
    if ($day30_2 < $day30_3) {
        $day302flag = '<font color="green">↓</font>';
    }
    if ($day30_2 == $day30_3) {
        $day302flag = '<font color="blue">-</font>';
    }

    $exp_int_time = strtotime($arrVpsData[$ip]['exp']);
    if (($exp_int_time - time()) / 86400 < 90) {
        if ($arrVpsData[$ip]) {
            $bg = 'bgcolor="#CFCCFC"';
        }
    } else {
        $bg = '';
    }

    $tableHead .= '
		<tr ' . $bg . '>
		<td width="175">' . $ip . '</td>
		<td width="70">' . $day7_1 . '[' . $day71flag . ']</td>
		<td width="70">' . $day30_1 . '[' . $day301flag . ']</td>
		<td width="70">' . $day7_2 . '[' . $day72flag . ']</td>
		<td width="70">' . $day30_2 . '[' . $day302flag . ']</td>
		<td width="70">' . $day7_3 . '</td>
		<td width="70">' . $day30_3 . '</td>
		<td width="100">' . $arrVpsData[$ip]['exp'] . '</td>
		<td width="100">' . $arrVpsData[$ip]['idc'] . '</td>
	</tr>	
	';
}



if ($IS_HEZUO) {
    $html = $tableHead . '</table><hr />';
    post_email_by_sae('合作建站-VPS-IP收入统计[' . date('Y-m-d H:i:s') . ']', $html, $arrHezuoUsers);
    post_email_by_smtp('合作建站-VPS-IP收入统计[' . date('Y-m-d H:i:s') . ']', $html, $arrHezuoUsers);
} else {
    $html = $tableHead . '</table><hr />' . PHP_EOL . '<pre>' . print_r($arrExtShouRuData, true) . '</pre>';
    post_email_by_sae('VPS-IP收入统计[' . date('Y-m-d H:i:s') . ']', $html, $arrUsers);
    post_email_by_smtp('VPS-IP收入统计[' . date('Y-m-d H:i:s') . ']', $html, $arrUsers);
}
?>